#include "queryresultmodel.h"

//QueryResultModel:QueryResultModel() {}
void QueryResultModel::OnRequestForQuery(){
    emit RequestForQuery(current,curpage,size);
}
QueryResultModel* QueryResultModel::getModel(QueryPackage* t){
    switch(t->qrytype){
    case OneWay:{
        auto k =new OneWayQueryResultModel(t);
        //k->Test();
        return k;
    }
    case RoundTrip:{
        auto k =  new RndTripResultModel(t);
        //k->Test();
        return k;
    }
    case Tranfer:{
        auto k =  new TransferResultModel(t);
        //k->Test();
        return k;
    }
    case Refund:{
        auto k = new RefundResultModel(t);
        k->Test();
        return k;
    }
    default:
        return new OneWayQueryResultModel(t);
    }
    return nullptr;
}

RefundResultModel::OrderDelegate::OrderDelegate(QObject *parent)
    : QStyledItemDelegate(parent),
    m_margin(10),
    m_padding(15),
    m_spacing(8),
    m_titleColor(51, 51, 51),
    m_contentColor(85, 85, 85),
    m_smallTextColor(136, 136, 136),
    m_separatorColor(221, 221, 221),
    m_backgroundColor(255, 255, 255),
    m_selectedBackgroundColor(240, 248, 255)
{
    // 初始化字体
    m_titleFont = QApplication::font();
    m_titleFont.setPointSize(m_titleFont.pointSize() + 2);
    m_titleFont.setBold(true);

    m_contentFont = QApplication::font();

    m_smallFont = QApplication::font();
    m_smallFont.setPointSize(m_smallFont.pointSize() - 1);
}

void RefundResultModel::OrderDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing, true);

    QRect itemRect = option.rect.adjusted(m_margin, m_margin, -m_margin, -m_margin);

    // 从模型获取订单数据
    QueryResult* order = index.data(RefundResultPtrRole).value<QueryResult*>();

    // 绘制卡片背景
    drawCardBackground(painter, itemRect, option);

    // 绘制订单信息
    drawOrderInfo(painter, itemRect.adjusted(m_padding, m_padding, -m_padding, -m_padding), order, option);

    painter->restore();
}

QSize RefundResultModel::OrderDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);

    // 计算每个item的固定高度
    int height = 2 * m_margin + 2 * m_padding +
                 QFontMetrics(m_titleFont).height() +
                 3 * QFontMetrics(m_contentFont).height() +
                 QFontMetrics(m_smallFont).height() +
                 4 * m_spacing;

    return QSize(option.rect.width(), height);
}

void RefundResultModel::OrderDelegate::drawCardBackground(QPainter *painter, const QRect &rect, const QStyleOptionViewItem &option) const
{
    painter->save();

    QPainterPath path;
    path.addRoundedRect(rect, 8, 8);

    // 根据选择状态设置背景色
    if (option.state & QStyle::State_Selected) {
        painter->fillPath(path, m_selectedBackgroundColor);
        // 绘制选中边框
        painter->setPen(QColor(120, 183, 255));
        painter->setBrush(Qt::NoBrush);
        painter->drawPath(path);
    } else {
        painter->fillPath(path, m_backgroundColor);
        // 绘制阴影效果
        QRect shadowRect = rect.translated(2, 2);
        QPainterPath shadowPath;
        shadowPath.addRoundedRect(shadowRect, 8, 8);
        painter->fillPath(shadowPath, QColor(0, 0, 0, 10));
    }

    painter->restore();
}

void RefundResultModel::OrderDelegate::drawOrderInfo(QPainter *painter, const QRect &rect, QueryResult* result, const QStyleOptionViewItem &option) const
{
    RefundResult* order = nullptr;
    if(!(order = dynamic_cast<RefundResult*>(result)))return;
    //qDebug()<<"Inside Delegate";
    painter->save();
    int x = rect.left();
    int y = rect.top();
    int width = rect.width();

    QFontMetrics titleFm(m_titleFont);
    QFontMetrics contentFm(m_contentFont);
    QFontMetrics smallFm(m_smallFont);

    // 1. 绘制车次信息（标题）
    painter->setFont(m_titleFont);
    painter->setPen(m_titleColor);
    QString trainInfo = QString("%1 (%2)").arg(order->trainname, order->Id);
    painter->drawText(x, y, width, titleFm.height(), Qt::AlignLeft | Qt::AlignTop, trainInfo);
    y += titleFm.height() + m_spacing;

    // 2. 绘制行程信息
    painter->setFont(m_contentFont);
    painter->setPen(m_contentColor);

    // 出发站和到达站
    QString stationInfo = QString("%1 → %2").arg(order->Source, order->Destination);
    painter->drawText(x, y, width, contentFm.height(), Qt::AlignLeft | Qt::AlignTop, stationInfo);

    // 出发时间和到达时间
    QString timeInfo = QString("%1 - %2").arg(order->SetOutTime.toString(), order->ArriveTime.toString());
    painter->drawText(x, y, width, contentFm.height(), Qt::AlignRight | Qt::AlignTop, timeInfo);
    y += contentFm.height() + m_spacing;

    // 绘制分隔线
    painter->setPen(m_separatorColor);
    painter->drawLine(x, y, x + width, y);
    y += m_spacing;

    // 3. 绘制订单详情（小字体）
    painter->setFont(m_smallFont);
    painter->setPen(m_smallTextColor);

    // 订单号和用户ID
    QString orderIdInfo = QString("订单号: %1").arg(order->orderId);
    QString userIdInfo = QString("用户ID: %1").arg(order->userId);

    int columnWidth = width / 2;
    painter->drawText(x, y, columnWidth, smallFm.height(), Qt::AlignLeft | Qt::AlignTop, orderIdInfo);
    painter->drawText(x + columnWidth, y, columnWidth, smallFm.height(), Qt::AlignRight | Qt::AlignTop, userIdInfo);
    y += smallFm.height() + m_spacing;

    // 下单时间和价格
    QString createTimeInfo = QString("创建时间: %1").arg(order->orderTime);
    QString priceInfo = QString("票价: ¥%1").arg(order->price);

    painter->drawText(x, y, columnWidth, smallFm.height(), Qt::AlignLeft | Qt::AlignTop, createTimeInfo);
    painter->drawText(x + columnWidth, y, columnWidth, smallFm.height(), Qt::AlignRight | Qt::AlignTop, priceInfo);

    painter->restore();
}
